package com.hfjyz.registration.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.hfjyz.registration.command.AdminLoginCommand;
import com.hfjyz.registration.command.DepartListCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.service.DepartService;

public class DepartListController extends SimpleFormController {

	private DepartService departService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		HttpSession session = request.getSession();
		Object cmd = session.getAttribute(getFormSessionAttributeName(request));
		if (cmd != null) {
			return cmd;
		}
		return super.formBackingObject(request);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#
	 * referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object,
	 * org.springframework.validation.Errors, int)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		DepartListCommand cmd = (DepartListCommand) command;
		String eid = request.getParameter("examId");
		if (eid != null && eid.length() > 0) {
			try {
				cmd.setExamId(Integer.valueOf(eid));
			} catch (Exception e) {
			}
		}
		return departService.initSearch(cmd, errors);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		DepartListCommand cmd = (DepartListCommand) command;
		request.getSession().setAttribute(getFormSessionAttributeName(), cmd);
		Map model = errors.getModel();
		if (request.getParameter("actionEditD") != null) {
			RedirectView rv = new RedirectView("departEdit.action");
			rv.setExposeModelAttributes(true);
			model.put("id", cmd.getDid());
			model.put("examId", cmd.getExamId());
			return new ModelAndView(rv, model);
		} else if (request.getParameter("btnAdd") != null) {
			RedirectView rv = new RedirectView("departEdit.action");
			rv.setExposeModelAttributes(true);
			model.put("examId", cmd.getExamId());
			return new ModelAndView(rv, model);
		} else if (request.getParameter("actionDeleteD") != null) {
			AdminLoginCommand adminCmd = (AdminLoginCommand) request
					.getSession().getAttribute("AdminInfo");
			departService.loggin(request, ExamContants.LOGING_TYPE_INFO,
					(Integer) null, ExamContants.LOGING_USER_TYPE_MANAGE,
					adminCmd.getAdminId(), ExamContants.LOGING_OP_TYPE_DELETE,
					"部门删除");
			departService.delete(request, cmd, errors);
		} else if (request.getParameter("actionShowOffice") != null) {
			RedirectView rv = new RedirectView("officeList.action");
			rv.setExposeModelAttributes(true);
			model.put("examId", cmd.getExamId());
			model.put("departId", cmd.getDid());
			return new ModelAndView(rv, model);
		}
		cmd.setDid(null);
		departService.search(request, response, cmd, errors);
		return new ModelAndView(getFormView(), model);
	}

	public DepartService getDepartService() {
		return departService;
	}

	public void setDepartService(DepartService departService) {
		this.departService = departService;
	}

}